#pragma once

#include "viml/compiler.h"
#include "viml/vm.h"
#include <gtest/gtest.h>
#include <chrono>

namespace viml {

/**
 * 函数调用测试基类
 */
class FunctionCallTestBase : public ::testing::Test {
protected:
    Compiler compiler;
    VM vm;
    
    void SetUp() override {
        compiler.reset();
        vm.reset();
    }
    
    void TearDown() override {
        // 清理工作
    }
    
    // 辅助方法：编译并执行源代码
    void compileAndExecute(const std::string& source, const std::string& testName = "test") {
        auto bytecode = compiler.compile(source, testName + ".vim");
        vm.execute(bytecode);
    }
    
    // 辅助方法：编译并执行源代码，返回结果
    Value executeWithResult(const std::string& source, const std::string& testName = "test") {
        auto bytecode = compiler.compile(source, testName + ".vim");
        return vm.execute(bytecode);
    }
    
    // 性能测试辅助结构
    struct PerformanceMetrics {
        size_t executionTime;      // 执行时间(微秒)
        size_t memoryUsage;        // 内存使用量(字节)
        size_t functionCalls;      // 函数调用次数
        size_t instructions;       // 执行指令数
        size_t gcCollections;      // 垃圾回收次数
        size_t objectsAllocated;   // 对象分配数量
    };
    
    // 执行性能测试
    PerformanceMetrics measurePerformance(const std::string& source, const std::string& testName = "test") {
        PerformanceMetrics metrics{};
        
        // 记录开始时间
        auto start = std::chrono::high_resolution_clock::now();
        
        // 重置统计信息
        vm.reset_statistics();
        
        // 执行测试
        try {
            auto bytecode = compiler.compile(source, testName + ".vim");
            vm.execute(bytecode);
        } catch (const std::exception& e) {
            ADD_FAILURE() << "Error in " << testName << ": " << e.what();
        }
        
        // 记录结束时间
        auto end = std::chrono::high_resolution_clock::now();
        auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
        
        // 收集性能数据
        metrics.executionTime = duration.count();
        const auto& stats = vm.get_statistics();
        metrics.functionCalls = stats.function_calls;
        metrics.instructions = stats.instructions_executed;
        
        return metrics;
    }
};

} // namespace viml